

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>LLVM Branch Weight Metadata &#8212; LLVM 9 documentation</title>
    <link rel="stylesheet" href="_static/llvm-theme.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
    <script src="_static/jquery.js"></script>
    <script src="_static/underscore.js"></script>
    <script src="_static/doctools.js"></script>
    <script src="_static/language_data.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="LLVM bugpoint tool: design and usage" href="Bugpoint.html" />
    <link rel="prev" title="LLVM Block Frequency Terminology" href="BlockFrequencyTerminology.html" />
<style type="text/css">
  table.right { float: right; margin-left: 20px; }
  table.right td { border: 1px solid #ccc; }
</style>

  </head><body>
<div class="logo">
  <a href="index.html">
    <img src="_static/logo.png"
         alt="LLVM Logo" width="250" height="88"/></a>
</div>

    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="Bugpoint.html" title="LLVM bugpoint tool: design and usage"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="BlockFrequencyTerminology.html" title="LLVM Block Frequency Terminology"
             accesskey="P">previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

        <li class="nav-item nav-item-this"><a href="">LLVM Branch Weight Metadata</a></li> 
      </ul>
    </div>


    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <div class="section" id="llvm-branch-weight-metadata">
<h1>LLVM Branch Weight Metadata<a class="headerlink" href="#llvm-branch-weight-metadata" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#introduction" id="id1">Introduction</a></p></li>
<li><p><a class="reference internal" href="#supported-instructions" id="id2">Supported Instructions</a></p>
<ul>
<li><p><a class="reference internal" href="#branchinst" id="id3"><code class="docutils literal notranslate"><span class="pre">BranchInst</span></code></a></p></li>
<li><p><a class="reference internal" href="#switchinst" id="id4"><code class="docutils literal notranslate"><span class="pre">SwitchInst</span></code></a></p></li>
<li><p><a class="reference internal" href="#indirectbrinst" id="id5"><code class="docutils literal notranslate"><span class="pre">IndirectBrInst</span></code></a></p></li>
<li><p><a class="reference internal" href="#callinst" id="id6"><code class="docutils literal notranslate"><span class="pre">CallInst</span></code></a></p></li>
<li><p><a class="reference internal" href="#other" id="id7">Other</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#built-in-expect-instructions" id="id8">Built-in <code class="docutils literal notranslate"><span class="pre">expect</span></code> Instructions</a></p>
<ul>
<li><p><a class="reference internal" href="#if-statement" id="id9"><code class="docutils literal notranslate"><span class="pre">if</span></code> statement</a></p></li>
<li><p><a class="reference internal" href="#switch-statement" id="id10"><code class="docutils literal notranslate"><span class="pre">switch</span></code> statement</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#cfg-modifications" id="id11">CFG Modifications</a></p></li>
<li><p><a class="reference internal" href="#function-entry-counts" id="id12">Function Entry Counts</a></p></li>
</ul>
</div>
<div class="section" id="introduction">
<h2><a class="toc-backref" href="#id1">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>Branch Weight Metadata represents branch weights as its likeliness to be taken
(see <a class="reference internal" href="BlockFrequencyTerminology.html"><span class="doc">LLVM Block Frequency Terminology</span></a>). Metadata is assigned to an
<code class="docutils literal notranslate"><span class="pre">Instruction</span></code> that is a terminator as a <code class="docutils literal notranslate"><span class="pre">MDNode</span></code> of the <code class="docutils literal notranslate"><span class="pre">MD_prof</span></code> kind.
The first operator is always a <code class="docutils literal notranslate"><span class="pre">MDString</span></code> node with the string
“branch_weights”.  Number of operators depends on the terminator type.</p>
<p>Branch weights might be fetch from the profiling file, or generated based on
<a class="reference internal" href="#builtin-expect">__builtin_expect</a> instruction.</p>
<p>All weights are represented as an unsigned 32-bit values, where higher value
indicates greater chance to be taken.</p>
</div>
<div class="section" id="supported-instructions">
<h2><a class="toc-backref" href="#id2">Supported Instructions</a><a class="headerlink" href="#supported-instructions" title="Permalink to this headline">¶</a></h2>
<div class="section" id="branchinst">
<h3><a class="toc-backref" href="#id3"><code class="docutils literal notranslate"><span class="pre">BranchInst</span></code></a><a class="headerlink" href="#branchinst" title="Permalink to this headline">¶</a></h3>
<p>Metadata is only assigned to the conditional branches. There are two extra
operands for the true and the false branch.</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>!0 = metadata !{
  metadata !&quot;branch_weights&quot;,
  i32 &lt;TRUE_BRANCH_WEIGHT&gt;,
  i32 &lt;FALSE_BRANCH_WEIGHT&gt;
}
</pre></div>
</div>
</div>
<div class="section" id="switchinst">
<h3><a class="toc-backref" href="#id4"><code class="docutils literal notranslate"><span class="pre">SwitchInst</span></code></a><a class="headerlink" href="#switchinst" title="Permalink to this headline">¶</a></h3>
<p>Branch weights are assigned to every case (including the <code class="docutils literal notranslate"><span class="pre">default</span></code> case which
is always case #0).</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>!0 = metadata !{
  metadata !&quot;branch_weights&quot;,
  i32 &lt;DEFAULT_BRANCH_WEIGHT&gt;
  [ , i32 &lt;CASE_BRANCH_WEIGHT&gt; ... ]
}
</pre></div>
</div>
</div>
<div class="section" id="indirectbrinst">
<h3><a class="toc-backref" href="#id5"><code class="docutils literal notranslate"><span class="pre">IndirectBrInst</span></code></a><a class="headerlink" href="#indirectbrinst" title="Permalink to this headline">¶</a></h3>
<p>Branch weights are assigned to every destination.</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>!0 = metadata !{
  metadata !&quot;branch_weights&quot;,
  i32 &lt;LABEL_BRANCH_WEIGHT&gt;
  [ , i32 &lt;LABEL_BRANCH_WEIGHT&gt; ... ]
}
</pre></div>
</div>
</div>
<div class="section" id="callinst">
<h3><a class="toc-backref" href="#id6"><code class="docutils literal notranslate"><span class="pre">CallInst</span></code></a><a class="headerlink" href="#callinst" title="Permalink to this headline">¶</a></h3>
<p>Calls may have branch weight metadata, containing the execution count of
the call. It is currently used in SamplePGO mode only, to augment the
block and entry counts which may not be accurate with sampling.</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>!0 = metadata !{
  metadata !&quot;branch_weights&quot;,
  i32 &lt;CALL_BRANCH_WEIGHT&gt;
}
</pre></div>
</div>
</div>
<div class="section" id="other">
<h3><a class="toc-backref" href="#id7">Other</a><a class="headerlink" href="#other" title="Permalink to this headline">¶</a></h3>
<p>Other terminator instructions are not allowed to contain Branch Weight Metadata.</p>
</div>
</div>
<div class="section" id="built-in-expect-instructions">
<span id="builtin-expect"></span><h2><a class="toc-backref" href="#id8">Built-in <code class="docutils literal notranslate"><span class="pre">expect</span></code> Instructions</a><a class="headerlink" href="#built-in-expect-instructions" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">__builtin_expect(long</span> <span class="pre">exp,</span> <span class="pre">long</span> <span class="pre">c)</span></code> instruction provides branch prediction
information. The return value is the value of <code class="docutils literal notranslate"><span class="pre">exp</span></code>.</p>
<p>It is especially useful in conditional statements. Currently Clang supports two
conditional statements:</p>
<div class="section" id="if-statement">
<h3><a class="toc-backref" href="#id9"><code class="docutils literal notranslate"><span class="pre">if</span></code> statement</a><a class="headerlink" href="#if-statement" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">exp</span></code> parameter is the condition. The <code class="docutils literal notranslate"><span class="pre">c</span></code> parameter is the expected
comparison value. If it is equal to 1 (true), the condition is likely to be
true, in other case condition is likely to be false. For example:</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">__builtin_expect</span><span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span> <span class="p">{</span>
  <span class="c1">// This block is likely to be taken.</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="switch-statement">
<h3><a class="toc-backref" href="#id10"><code class="docutils literal notranslate"><span class="pre">switch</span></code> statement</a><a class="headerlink" href="#switch-statement" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">exp</span></code> parameter is the value. The <code class="docutils literal notranslate"><span class="pre">c</span></code> parameter is the expected
value. If the expected value doesn’t show on the cases list, the <code class="docutils literal notranslate"><span class="pre">default</span></code>
case is assumed to be likely taken.</p>
<div class="highlight-c++ notranslate"><div class="highlight"><pre><span></span><span class="k">switch</span> <span class="p">(</span><span class="n">__builtin_expect</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span> <span class="p">{</span>
<span class="k">default</span><span class="o">:</span> <span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="mi">0</span><span class="o">:</span>  <span class="c1">// ...</span>
<span class="k">case</span> <span class="mi">3</span><span class="o">:</span>  <span class="c1">// ...</span>
<span class="k">case</span> <span class="mi">5</span><span class="o">:</span>  <span class="c1">// This case is likely to be taken.</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="cfg-modifications">
<h2><a class="toc-backref" href="#id11">CFG Modifications</a><a class="headerlink" href="#cfg-modifications" title="Permalink to this headline">¶</a></h2>
<p>Branch Weight Metatada is not proof against CFG changes. If terminator operands’
are changed some action should be taken. In other case some misoptimizations may
occur due to incorrect branch prediction information.</p>
</div>
<div class="section" id="function-entry-counts">
<h2><a class="toc-backref" href="#id12">Function Entry Counts</a><a class="headerlink" href="#function-entry-counts" title="Permalink to this headline">¶</a></h2>
<p>To allow comparing different functions during inter-procedural analysis and
optimization, <code class="docutils literal notranslate"><span class="pre">MD_prof</span></code> nodes can also be assigned to a function definition.
The first operand is a string indicating the name of the associated counter.</p>
<p>Currently, one counter is supported: “function_entry_count”. The second operand
is a 64-bit counter that indicates the number of times that this function was
invoked (in the case of instrumentation-based profiles). In the case of
sampling-based profiles, this operand is an approximation of how many times
the function was invoked.</p>
<p>For example, in the code below, the instrumentation for function foo()
indicates that it was called 2,590 times at runtime.</p>
<div class="highlight-llvm notranslate"><div class="highlight"><pre><span></span><span class="k">define</span> <span class="k">i32</span> <span class="vg">@foo</span><span class="p">()</span> <span class="nv">!prof</span> <span class="nv nv-Anonymous">!1</span> <span class="p">{</span>
  <span class="k">ret</span> <span class="k">i32</span> <span class="m">0</span>
<span class="p">}</span>
<span class="nv nv-Anonymous">!1</span> <span class="p">=</span> <span class="p">!{</span><span class="nv">!&quot;function_entry_count&quot;</span><span class="p">,</span> <span class="k">i64</span> <span class="m">2590</span><span class="p">}</span>
</pre></div>
</div>
<p>If “function_entry_count” has more than 2 operands, the later operands are
the GUID of the functions that needs to be imported by ThinLTO. This is only
set by sampling based profile. It is needed because the sampling based profile
was collected on a binary that had already imported and inlined these functions,
and we need to ensure the IR matches in the ThinLTO backends for profile
annotation. The reason why we cannot annotate this on the callsite is that it
can only goes down 1 level in the call chain. For the cases where
foo_in_a_cc()-&gt;bar_in_b_cc()-&gt;baz_in_c_cc(), we will need to go down 2 levels
in the call chain to import both bar_in_b_cc and baz_in_c_cc.</p>
</div>
</div>


            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="Bugpoint.html" title="LLVM bugpoint tool: design and usage"
             >next</a> |</li>
        <li class="right" >
          <a href="BlockFrequencyTerminology.html" title="LLVM Block Frequency Terminology"
             >previous</a> |</li>
  <li><a href="http://llvm.org/">LLVM Home</a>&nbsp;|&nbsp;</li>
  <li><a href="index.html">Documentation</a>&raquo;</li>

        <li class="nav-item nav-item-this"><a href="">LLVM Branch Weight Metadata</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2003-2020, LLVM Project.
      Last updated on 2020-09-20.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.2.1.
    </div>
  </body>
</html>